package com.way.service;

import com.way.api.system.RemoteUserService;
import com.way.constants.CommonConstants;
import com.way.domain.system.SysUser;
import com.way.exception.CommonException;
import com.way.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class LoginService {

    @Autowired
    private RemoteUserService  userService;

    public SysUser login(String username, String password) {
        SysUser user = userService.getUser(username);
        if (user == null) {
            throw new CommonException("用户名不存在");
        }
        if (CommonConstants.USER_DELETE_FLAG.equals(user.getDelFlag())) {
            throw new CommonException("用户失效");
        }
        if (CommonConstants.USER_DISABLE_FLAG.equals(user.getStatus())) {
            throw new CommonException("用户失效");
        }
        if (!PasswordUtil.matches(user, password)) {
            throw new CommonException("密码错误");
        }
        return user;
    }
}